﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using PMisuseAn.io;
using PMisuseAn.behaviour.model;

namespace PMisuseAn
{
    /// <summary>
    /// Interaction logic for GroupConfigWindow.xaml
    /// </summary>
    public partial class GroupConfigWindow : Window
    {
        Group group_;
        public static List<string> groupNames;

        public GroupConfigWindow()
        {
            InitializeComponent();
            loadGroups_cb();            
        }

        private void add_agent_b_Click(object sender, RoutedEventArgs e)
        {
            if (group_ == null)
            {
                if (group_name_cb.Text == null || group_name_cb.Text.Trim().Equals(""))
                {
                    MessageBox.Show("Missing group name...", "Error", MessageBoxButton.OK, MessageBoxImage.Information);
                    return;
                }

                string groupName = null;
                //if (group_name_cb.SelectedItem != null)
                //{
                //    groupName = group_name_cb.SelectedItem.ToString();
                //}
                //else
                //{
                //    groupName = group_name_cb.Text;
                //}

                groupName = group_name_cb.Text;

                if (groupName == null || groupName.Equals(""))
                {
                    groupName = group_name_cb.SelectedItem.ToString();
                }


                groupName = groupName.Trim();
                groupName = groupName.TrimEnd('\n');
                groupName = groupName.TrimEnd('\r');

                group_ = MisUseAnObjectsDB.loadGroupDB(groupName, MainWindow.clusterName);

                if (group_ == null)
                {
                    group_ = new Group();
                    group_.groupName = groupName;
                }             
            }
            AddAgentWindow aaw = new AddAgentWindow(group_);
            aaw.Owner = (Window)this.Parent;
            aaw.ShowDialog();
            group_name_cb.IsEnabled = false;
            loadGroupMembers();
        }

        public void loadGroups_cb()
        {
            groupNames = MisUseAnObjectsDB.loadAllGroupNamesDB(MainWindow.clusterName);
            group_name_cb.ItemsSource = groupNames;
        }

        public void loadGroupMembers()
        {
            int a_no = 1;
            groupMembers_lb.Items.Clear();
            foreach(Agent agent in group_.members)
            {
                ListBoxItem l = new ListBoxItem();
                StackPanel s = new StackPanel();
                s.Orientation = System.Windows.Controls.Orientation.Horizontal;

                ContentPresenter c1 = new ContentPresenter();
                c1.Content = a_no+ ".";
                a_no = a_no + 1;

                Button open_agent_b = new Button();
                open_agent_b.Tag = agent;
                open_agent_b.Style = (System.Windows.Style)FindResource("LinkButton");
                open_agent_b.Height = 16;
                open_agent_b.FontSize = 11;
                open_agent_b.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
                open_agent_b.VerticalAlignment = System.Windows.VerticalAlignment.Top;
                open_agent_b.Content = agent.agentName;
                open_agent_b.AddHandler(Button.ClickEvent, new RoutedEventHandler(open_agent_b_Click));
                ContentPresenter c2 = new ContentPresenter();
                c2.Content = open_agent_b;

                c1.Width = 15;
                c2.Width = groupMembers_lb.Width - 30;
                
                s.Children.Add(c1);
                s.Children.Add(c2);

                ContextMenu cm = new System.Windows.Controls.ContextMenu();
                cm.Height = 64;
                cm.Width = 136;

                MenuItem cm_d = new MenuItem();
                cm_d.Header = "Delete";
                cm_d.Tag = agent;
                cm_d.AddHandler(MenuItem.ClickEvent, new RoutedEventHandler(del_agent_from_group_menu_Clicked));
                cm_d.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
                cm_d.HorizontalContentAlignment = System.Windows.HorizontalAlignment.Right;
                cm_d.FontSize = 12;
                cm_d.Foreground = Brushes.DarkGreen;

                BitmapImage cm_d_image = new BitmapImage();
                cm_d_image.BeginInit();
                Uri cm_d_Uri = new Uri("/PMisuseAn;component/images/DeleteHS.png", UriKind.RelativeOrAbsolute);
                cm_d_image.UriSource = cm_d_Uri;
                cm_d_image.EndInit();

                Image cm_d_iconImage = new Image();
                cm_d_iconImage.Source = cm_d_image;
                cm_d.Icon = cm_d_iconImage;

                cm.Items.Add(cm_d);
                l.ContextMenu = cm;

                l.Content = s;

                groupMembers_lb.Items.Add(l); 
            }

            if (group_.groupType == Group.GroupType.Planned)
            {
                group_type_planned.IsChecked = true;
            }
            else
            {
                group_type_emergent.IsChecked = true;
            }
        }

        public void del_agent_from_group_menu_Clicked(object sender, RoutedEventArgs e)
        {
            MessageBoxResult result = MessageBox.Show("Confirm delete...", "Delete", MessageBoxButton.YesNo, MessageBoxImage.Question);
            if (result == MessageBoxResult.No)
            {
                return;
            }

            MenuItem cm_i = (MenuItem)sender;
            Agent agent = (Agent)cm_i.Tag;
            MisUseAnObjectsDB.deleteGroupAgentDB(agent.agentName, group_.groupName, MainWindow.clusterName);
            group_.removeAgent(agent);
            loadGroupMembers();
        }

        public void open_agent_b_Click(object sender, RoutedEventArgs e)
        {
            Button cm_i = (Button)sender;
            Agent agent = (Agent)cm_i.Tag;
            AddAgentWindow aaw = new AddAgentWindow(agent);
            aaw.Owner = (Window)this.Parent;
            aaw.ShowDialog();
        }
        

        private void groupName_cb_selection_changed(object sender, SelectionChangedEventArgs e)
        {
            string groupName = null;
            if (group_name_cb.SelectedItem != null)
            {
                groupName = group_name_cb.SelectedItem.ToString();
            }
            else
            {
                groupName = group_name_cb.Text;
            }
            
            groupName = groupName.Trim();
            groupName = groupName.TrimEnd('\n');
            groupName = groupName.TrimEnd('\r');

            if (groupName != null && groupName.Count() > 0)
            {
                group_ = MisUseAnObjectsDB.loadGroupDB(groupName, MainWindow.clusterName);
            }
            if (group_ != null)
            {
                loadGroupMembers();

                if (group_.groupType == Group.GroupType.Planned)
                {
                    group_type_planned.IsChecked = true;
                }
                else
                {
                    group_type_emergent.IsChecked = true;
                }
            }
            else
            {
                group_ = null;
                groupMembers_lb.Items.Clear();
            }

        }

        private void key_pressed_groupName_cb(object sender, KeyEventArgs e)
        {
            //string groupName = null;
            //if (group_name_cb.SelectedItem != null)
            //{
            //    groupName = group_name_cb.SelectedItem.ToString();
            //}
            //else
            //{
            //    groupName = group_name_cb.Text;
            //}

            //groupName = groupName.Trim();
            //groupName = groupName.TrimEnd('\n');
            //groupName = groupName.TrimEnd('\r');
            //foreach (string g_n in groupNames)
            //{
            //    if (g_n.Equals(groupName))
            //    {
            //        group_ = MisUseAnObjectsDB.loadGroupDB(groupName, MainWindow.clusterName);
            //        break;
            //    }
            //}

            //if (group_ != null)
            //{
            //    loadGroupMembers();
            //}
            //else
            //{
            group_ = null;
            groupMembers_lb.Items.Clear();
            //}
        }

        private void ok_group_b_Click(object sender, RoutedEventArgs e)
        {
            if (group_name_cb.Text == null || group_name_cb.Text.Trim().Equals(""))
            {
                MessageBox.Show("Missing group name...", "Error", MessageBoxButton.OK, MessageBoxImage.Information);
                return;
            }

            string groupName = null;
            //if (group_name_cb.SelectedItem != null)
            //{
            //    groupName = group_name_cb.SelectedItem.ToString();
            //}
            //else
            //{
            //    groupName = group_name_cb.Text;
            //}

            groupName = group_name_cb.Text;

            if (groupName == null || groupName.Equals(""))
            {
                groupName = group_name_cb.SelectedItem.ToString();
            }

            groupName = groupName.Trim();
            groupName = groupName.TrimEnd('\n');
            groupName = groupName.TrimEnd('\r');

            if (group_ == null)
            {
                group_ = new Group();
                group_.groupName = groupName;
            }

            //foreach (string g_s in groupNames)
            //{
            //    if(groupName.Equals(g_s))
            //    {
            //        MessageBox.Show("Group already exist.", "Information", MessageBoxButton.OK, MessageBoxImage.Information);
            //        return;
            //    }
            //}
            if (group_type_planned.IsChecked == true)
            {
                group_.groupType = Group.GroupType.Planned;
            }
            else
            {
                group_.groupType = Group.GroupType.Emergent;
            }

            MisUseAnObjectsDB.addGroupDB(group_, MainWindow.clusterName);
            this.Close();
        }

        private void delete_group_b_Click(object sender, RoutedEventArgs e)
        {
            if (group_name_cb.Text == null || group_name_cb.Text.Trim().Equals(""))
            {
                MessageBox.Show("Missing group name...", "Error", MessageBoxButton.OK, MessageBoxImage.Information);
                return;
            }

            string groupName = null;
            //if (group_name_cb.SelectedItem != null)
            //{
            //    groupName = group_name_cb.SelectedItem.ToString();
            //}
            //else
            //{
            //    groupName = group_name_cb.Text;
            //}

            groupName = group_name_cb.Text;

            if (groupName == null || groupName.Equals(""))
            {
                groupName = group_name_cb.SelectedItem.ToString();
            }



            groupName = groupName.Trim();
            groupName = groupName.TrimEnd('\n');
            groupName = groupName.TrimEnd('\r');

            MisUseAnObjectsDB.deleteGroupDB(groupName, MainWindow.clusterName);
            group_name_cb.Text = "";
            groupMembers_lb.Items.Clear();
            loadGroups_cb();
        }
    }
}
